package com.imau.service.imp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imau.bean.Department;
import com.imau.common.dataSourceTool.SqlSessionFactoryUtil;
import com.imau.common.entiry.PageQuery;
import com.imau.common.utils.StringUtils;
import com.imau.common.utils.TreeBuildUtils;
import com.imau.mapper.DepartmentMapper;
import com.imau.service.DepartmentService;
import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public class DepartmentServiceImp extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    //操作数据库的session
    private SqlSession session;


    public DepartmentServiceImp() {
        //父类 ServiceImpl属性，自动注入
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        this.baseMapper = session.getMapper(DepartmentMapper.class);
    }

    @Override
    public Department queryById(Serializable id) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        return this.baseMapper.selectById(id);
    }

    @Override
    public List<Department> queryList(Department entity) {
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<Department> lqw = buildQueryWrapper(entity);

        //2.调用方法

        return super.list(lqw);
    }

    @Override
    public Page<Department> queryPageList(Department entity, PageQuery pageQuery) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<Department> lqw = buildQueryWrapper(entity);


        return super.page(pageQuery.build(), lqw);
    }

    @Override
    public boolean updateByEntity(Department entity) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.updateById(entity);
    }

    @Override
    public boolean saveEntity(Department entity) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return super.save(entity);
    }

    @Override
    public boolean removeWithById(Serializable id) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.removeById(id);
    }

    @Override
    public boolean removeWithValidByIds(Collection<?> ids) {
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.removeBatchByIds(ids);
    }

    @Override
    public LambdaQueryWrapper<Department> buildQueryWrapper(Department dept) {

        //根据字段类别
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(dept.getDid()), Department::getDid, dept.getDid())
                .eq(ObjectUtil.isNotNull(dept.getParentId()), Department::getParentId, dept.getParentId())
                .like(StringUtils.isNotBlank(dept.getDeptName()), Department::getDeptName, dept.getDeptName())
                .eq(StringUtils.isNotBlank(dept.getStatus()), Department::getStatus, dept.getStatus())
                .orderByAsc(Department::getParentId)
                .orderByAsc(Department::getOrderNum);
        return lqw;

    }


    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    public List<Tree<String>> buildDeptTreeSelect(List<Department> depts) {
        if (CollUtil.isEmpty(depts)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(depts, (dept, tree) ->
                tree.setId(dept.getDid())
                        .setParentId(dept.getParentId())
                        .setName(dept.getDeptName())
                        .setWeight(dept.getOrderNum()));
    }

    public List<Tree<String>> selectDeptTreeList(Department dept){
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        return  this.buildDeptTreeSelect(this.queryList(dept));
    }
}
